/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "visualeffect.h"
#include "gameengine.h"
#include "timermanager.h"
#include "spacecraft.h" // for SpaceCraftEffect and extending classes
#include "visualeffectdata.h"

#include <QDebug>

/*!
  \class BallToConeEffect
  VisualEffect when miner transforms from ball to cone mode.
*/

/*!
  Constructs the effect.

  \a frame Current frame.

  \a timeStamp Current time.

  \a miner
*/
BallToConeEffect::BallToConeEffect(int frame, FPtype timeStamp, SpaceCraft *miner)
    : GameEffect(frame, timeStamp, GameEffect::SpaceCraftEffect)
    , miner(miner)
{
}

/*!
  Starts the effect.
*/
void BallToConeEffect::start(int)
{
    TimerManager * timerManager = TimerManager::getInstance();

    const bool clockOpposite = timerManager->isActive(GameTimer::shipCone2BallTimer);

#ifdef SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    const bool clockSelf = timerManager->isActive(GameTimer::shipBall2ConeTimer);

    if (clockSelf && clockOpposite) {
        qDebug() << "BallToConeEffect::start() : both clocks running!";

        // this condition may happen when we start the rewind mode
        // -> just print a notice without exiting the program.
    }

    if (clockSelf) {
        qDebug() << "BallToConeEffect::start() : the clock is already running!";

        // this condition may happen when we start the rewind mode
        // -> just print a notice without exiting the program.
    }

#endif  // SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    if (clockOpposite) {

        // transition from cone to ball is running, and we need to reverse it.
        //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

        // get the current value from Cone2Ball timer.
        const int c2b = timerManager->time(GameTimer::shipCone2BallTimer);
        const int startTime = timerManager->timerLength(GameTimer::shipCone2BallTimer) - c2b;

        // start Ball2Cone timer with remaining time from Cone2Ball timer
        timerManager->startGameTimer(GameTimer::shipBall2ConeTimer, startTime);
        timerManager->stopGameTimer(GameTimer::shipCone2BallTimer);
    } else {
        timerManager->startGameTimer(GameTimer::shipBall2ConeTimer);
    }
}

/*!
  \class ConeToBallEffect
  VisualEffect when miner transforms from cone to ball mode.
*/

/*!
  Constructs the effect.

  \a frame Current frame.

  \a timeStamp Current time.

  \a miner
*/
ConeToBallEffect::ConeToBallEffect(int frame, FPtype timeStamp, SpaceCraft *miner)
    : GameEffect(frame, timeStamp, GameEffect::SpaceCraftEffect)
    , miner(miner)
{
}

/*!
  Starts the effect.
*/
void ConeToBallEffect::start(int)
{
    TimerManager * timerManager = TimerManager::getInstance();

    const bool clockOpposite = timerManager->isActive(GameTimer::shipBall2ConeTimer);

#ifdef SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    const bool clockSelf = timerManager->isActive(GameTimer::shipCone2BallTimer);

    if (clockSelf && clockOpposite) {
        qDebug() << "ConeToBallEffect::start() : both clocks running!";

        // this condition may happen when we start the rewind mode
        // -> just print a notice without exiting the program.
    }

    if (clockSelf) {
        qDebug() << "ConeToBallEffect::start() : the clock is already running!";

        // this condition may happen when we start the rewind mode
        // -> just print a notice without exiting the program.
    }

#endif  // SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    if (clockOpposite) {

        // transition from ball to cone is running, and we need to reverse it.
        //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

        // get the current value from Ball2Cone timer.
        const int b2c = timerManager->time(GameTimer::shipBall2ConeTimer);
        const int startTime = timerManager->timerLength(GameTimer::shipBall2ConeTimer) - b2c;

        // start Cone2Ball timer with remaining time from Ball2Cone timer.
        timerManager->startGameTimer(GameTimer::shipCone2BallTimer, startTime);
        timerManager->stopGameTimer(GameTimer::shipBall2ConeTimer);
    }
    else {
        timerManager->startGameTimer(GameTimer::shipCone2BallTimer);
    }
}

/*!
  \class HyperJumpOutEffect
  VisualEffect when miner hyper jumps out from game screen.
*/

/*!
  Constructs the effect.

  \a frame Current frame.

  \a timeStamp Current time.

  \a posX X position of hyperjump out.

  \a posY Y position of hyperjump out.
*/
HyperJumpOutEffect::HyperJumpOutEffect(int frame, FPtype timeStamp, FPtype posX, FPtype posY)
    : GameEffect(frame, timeStamp, GameEffect::HyperJumpOutEffect)
    , outX(posX)
    , outY(posY)
{
}

/*!
  Starts the effect.
*/
void HyperJumpOutEffect::start(int msecs)
{
    TimerManager * timerManager = TimerManager::getInstance();

#ifdef SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    if (timerManager->isActive(GameTimer::hyperJumpOutTimer)) {
        qDebug() << "HyperJumpOutEffect::start() : timer already running.";
        exit(EXIT_FAILURE);
    }

    if (TimerManager::hyperJumpOutData != NULL) {
        qDebug() << "HyperJumpOutEffect::start() : hyperJumpOutData != NULL";
        exit(EXIT_FAILURE);
    }

#endif  // SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    timerManager->startGameTimer(GameTimer::hyperJumpOutTimer, msecs);
    TimerManager::hyperJumpOutData = new HyperJumpEffectData(outX, outY, false);
}

/*!
  \class HyperJumpInEffect
  VisualEffect when miner hyper jumps in the game screen.
*/

/*!
  Constructs the effect.

  \a frame Current frame.

  \a timeStamp Current time.

  \a posX X position of hyperjump in.

  \a posY Y position of hyperjump in.
*/
HyperJumpInEffect::HyperJumpInEffect(int frame, FPtype timeStamp, FPtype posX, FPtype posY)
    : GameEffect(frame, timeStamp, GameEffect::HyperJumpInEffect)
    , inX(posX)
    , inY(posY)
{
}

/*!
  Starts the effect.
*/
void HyperJumpInEffect::start(int msecs)
{
    TimerManager * timerManager = TimerManager::getInstance();

#ifdef SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    if (timerManager->isActive(GameTimer::hyperJumpInTimer)) {
        qDebug() << "HyperJumpInEffect::start() : timer already running.";
        exit(EXIT_FAILURE);
    }

    if (TimerManager::hyperJumpInData != NULL) {
        qDebug() << "HyperJumpInEffect::start() : hyperJumpInData != NULL";
        exit(EXIT_FAILURE);
    }

#endif  // SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    timerManager->startGameTimer(GameTimer::hyperJumpInTimer, msecs);
    TimerManager::hyperJumpInData = new HyperJumpEffectData(inX, inY, true);
}

/*!
  \class BombEffect
  VisualEffect when bomb is launched.
*/

/*!
  Constructs the effect.

  \a frame Current frame.

  \a timeStamp Current time.

  \a posX X position of bomb launch.

  \a posY Y position of bomb launch.
*/
BombEffect::BombEffect(int frame, FPtype timestamp, FPtype posX, FPtype posY)
    : GameEffect(frame, timestamp, GameEffect::BombEffect)
    , posX(posX)
    , posY(posY)
{
}

/*!
  Starts the effect.
*/
void BombEffect::start(int)
{
    TimerManager * timerManager = TimerManager::getInstance();

#ifdef SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    if (timerManager->isActive(GameTimer::bombEffectTimer)) {
        qDebug() << "BombEffect::start() : timer already running.";
        exit(EXIT_FAILURE);
    }

    if (TimerManager::bombEffectData != NULL) {
        qDebug() << "BombEffect::start() : hyperJumpInData != NULL";
        exit(EXIT_FAILURE);
    }

#endif  // SMASHMINER_CONFIG_ENABLE_DEBUG_CHECKS

    timerManager->startGameTimer(GameTimer::bombEffectTimer);
    TimerManager::bombEffectData = new BombEffectData(posX, posY);
}
